package cn.xiaye.blog.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.xiaye.blog.bean.Article;
import cn.xiaye.blog.db.util.DbUtil;

public class ArticleDao {
	DbUtil dbUtil = DbUtil.getDefaultDatabaseUtil();

	public void insert(Article article) {
		PreparedStatement ps = null;
		ps = dbUtil.prepareStatement("insert into article(name,text,date,times,sort_id) values(?,?,?,?,?)");
		try {
			ps.setString(1, article.getName());
			ps.setString(2, article.getText());
			ps.setDate(3, article.getDate());
			ps.setInt(4, article.getTimes());
			ps.setInt(5, article.getSortId());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			dbUtil.close(ps);
		}

	}

	public void delete(int articleId) {
		PreparedStatement ps = null;
		ps = dbUtil.prepareStatement("delete from article where id=?");
		try {
			ps.setInt(1, articleId);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			dbUtil.close(ps);
		}
	}

	public void update(int sortId,String text,String name) {
		PreparedStatement ps = null;
		ps = dbUtil.prepareStatement("update article set sort_id=?,text=? where name=?" );
		try {
			ps.setInt(1, sortId);
			ps.setString(2, text);
			ps.setString(3, name);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			dbUtil.close(ps);
		}
	}

	public List<Article> queryAll() {
		List<Article> articleList = new ArrayList<Article>();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = dbUtil
					.prepareStatement("select art.*,st.name as sort_name from article art left join sort st on (art.sort_id=st.id)order by id desc;");
			rs = ps.executeQuery();
			while (rs.next()) {
				Article article = new Article();
				article.setId(rs.getInt("id"));
				article.setName(rs.getString("name"));
				article.setText(rs.getString("text"));
				article.setDate(rs.getDate("date"));
				article.setTimes(rs.getInt("times"));
				article.setSortId(rs.getInt("sort_id"));
				article.setSortName(rs.getString("sort_name"));
				articleList.add(article);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			dbUtil.close(ps);
			dbUtil.close(rs);
		}
		return articleList;
	}

	public List<Article> queryBySortId(int sortId) {
		List<Article> articleList = new ArrayList<Article>();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = dbUtil
					.prepareStatement("select art.*,st.name as sort_name from article art left join sort st on (art.sort_id=st.id) where sort_id=? order by id desc;");
			ps.setInt(1, sortId);
			rs = ps.executeQuery();
			while (rs.next()) {
				Article article = new Article();
				article.setId(rs.getInt("id"));
				article.setName(rs.getString("name"));
				article.setText(rs.getString("text"));
				article.setDate(rs.getDate("date"));
				article.setTimes(rs.getInt("times"));
				article.setSortId(rs.getInt("sort_id"));
				article.setSortName(rs.getString("sort_name"));
				articleList.add(article);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			dbUtil.close(ps);
			dbUtil.close(rs);
		}
		return articleList;
	}
	public Article queryByArticleId(int articleId){
		Article article = new Article();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = dbUtil
					.prepareStatement("select art.*,st.name as sort_name from article art left join sort st on (art.sort_id=st.id) where art.id=?");
			ps.setInt(1, articleId);
			rs = ps.executeQuery();
			while (rs.next()) {
				article.setId(rs.getInt("id"));
				article.setName(rs.getString("name"));
				article.setText(rs.getString("text"));
				article.setDate(rs.getDate("date"));
				article.setTimes(rs.getInt("times"));
				article.setSortId(rs.getInt("sort_id"));
				article.setSortName(rs.getString("sort_name"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			dbUtil.close(ps);
			dbUtil.close(rs);
		}
		return article;
	}
}
